<script setup>
import { ref } from 'vue'
import { feedbackPageQueryService } from '@/api/feedback.js'
import { Search, Refresh, DCaret, CaretTop, CaretBottom } from '@element-plus/icons-vue'
import ProjectTypeSelect from '../project/components/ProjectTypeSelect.vue'

const feedbackList = ref([]) // 评价列表
const total = ref(0) // 总条数
const loading = ref(false) // loading状态

// 请求参数
const params = ref({
  pageNum: 1, // 当前页码
  pageSize: 10, // 每页条数
  orderBy: 'update_time', // 排序字段
  isAsc: false, // 是否升序
  projectName: null, // 服务项目名称
  projectTypeId: null, // 服务项目类型id
  employeeName: null, // 员工姓名
  memberName: null, // 会员姓名
  plateNumber: null // 车牌号
})

// 评价状态与el-tag类型映射
const feedbackStatusTagTypeMap = {
  已评价: 'success',
  待评价: 'danger'
}
const getFeedbackStatusTagType = (status) => {
  return feedbackStatusTagTypeMap[status] || 'info'
}

// 基于params参数，获取评价列表
const getFeedbackList = async () => {
  loading.value = true
  const res = await feedbackPageQueryService(params.value)
  feedbackList.value = res.data.data.list
  total.value = res.data.data.total
  loading.value = false
}
getFeedbackList()

// 处理分页逻辑
const onPageSizeChange = (size) => {
  params.value.pageNum = 1
  params.value.pageSize = size
  getFeedbackList()
}
const onPageNumChange = (page) => {
  params.value.pageNum = page
  getFeedbackList()
}

// 处理排序逻辑
const handleSort = (field) => {
  if (params.value.isAsc === false && params.value.orderBy === field) {
    // 如果当前已经降序，则重置排序
    params.value.orderBy = null
    params.value.isAsc = null
  } else if (params.value.orderBy === field) {
    // 如果当前已经是按该字段排序，则切换升序/降序
    params.value.isAsc = !params.value.isAsc
  } else {
    // 如果是第一次点击该字段，则默认升序
    params.value.orderBy = field
    params.value.isAsc = true
  }
  // 重新获取数据
  getFeedbackList()
}

// 获取排序图标
const getSortIcon = (field) => {
  if (params.value.orderBy === field) {
    return params.value.isAsc ? CaretTop : CaretBottom
  }
  return DCaret
}

// 显示业务信息
const activeBusinessId = ref() // 当前需要显示的业务ID
const showBusinessInfoForm = (businessId) => {
  activeBusinessId.value = businessId
}

// 搜索逻辑
const handleSearch = () => {
  params.value.pageNum = 1
  getFeedbackList()
}

// 重置逻辑
const handleReset = () => {
  params.value = {
    ...params.value, // 保留其他属性
    pageNum: 1,
    projectName: null,
    projectTypeId: null,
    employeeName: null,
    memberName: null,
    plateNumber: null
  }
  getFeedbackList()
}
</script>

<template>
  <page-container title="评价管理">
    <!-- 表单 -->
    <el-form inline>
      <el-form-item label="服务项目名称:"
        ><el-input v-model="params.projectName" placeholder="请输入"
      /></el-form-item>
      <el-form-item label="服务项目类型:">
        <project-type-select v-model="params.projectTypeId" width="160px" />
      </el-form-item>
      <el-form-item label="员工姓名:"
        ><el-input v-model="params.employeeName" placeholder="请输入"
      /></el-form-item>
      <el-form-item label="会员姓名:"
        ><el-input v-model="params.memberName" placeholder="请输入"
      /></el-form-item>
      <el-form-item label="车牌号:"
        ><el-input v-model="params.plateNumber" placeholder="请输入"
      /></el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="handleSearch"></el-button>
        <el-button type="default" :icon="Refresh" @click="handleReset"></el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table class="tb" :data="feedbackList" v-loading="loading" :row-style="{ height: '50px' }">
      <el-table-column prop="id" label="评价ID" width="150">
        <template #header>
          <span>评价ID</span>
          <el-icon @click="handleSort('id')">
            <component :is="getSortIcon('id')" />
          </el-icon>
        </template>
      </el-table-column>
      <el-table-column prop="businessId" label="业务ID" width="180">
        <template #header>
          <span>业务ID</span>
          <el-icon @click="handleSort('business_id')">
            <component :is="getSortIcon('business_id')" />
          </el-icon>
        </template>
        <template #default="{ row }">
          <el-popover effect="light" trigger="click" placement="right" :width="600">
            <template #reference>
              <el-tag @click="showBusinessInfoForm(row.businessId)">{{ row.businessId }}</el-tag>
            </template>
            <BusinessInfoForm
              v-if="activeBusinessId === row.businessId"
              :businessId="row.businessId"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column prop="rating" label="评分" width="180">
        <template #default="{ row }">
          <el-rate
            v-model="row.rating"
            disabled
            show-score
            size="small"
            text-color="#ff9900"
            score-template="{value}分"
          />
        </template>
      </el-table-column>
      <el-table-column prop="content" label="评价内容" width="400" show-overflow-tooltip />
      <el-table-column prop="status" label="评价状态" width="180">
        <template #default="{ row }">
          <el-tag :type="getFeedbackStatusTagType(row.status)">{{ row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="180">
        <template #header>
          <span>创建时间</span>
          <el-icon @click="handleSort('create_time')">
            <component :is="getSortIcon('create_time')" />
          </el-icon>
        </template>
      </el-table-column>
      <el-table-column prop="updateTime" label="更新时间" width="180">
        <template #header>
          <span>更新时间</span>
          <el-icon @click="handleSort('update_time')">
            <component :is="getSortIcon('update_time')" />
          </el-icon>
        </template>
      </el-table-column>
      <!-- 空数据提示 -->
      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      v-model:current-page="params.pageNum"
      v-model:page-size="params.pageSize"
      :page-sizes="[5, 10, 20, 50, 100]"
      :background="true"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="onPageSizeChange"
      @current-change="onPageNumChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
  </page-container>
</template>

<style lang="scss" scoped>
.tb {
  border-radius: 10px;
  box-shadow: 0 5px 10px 0 rgba(0, 0, 0, 0.05);
  overflow: hidden;
}
</style>
